#include "../include/Root.h"

using namespace godot;

void Root::_register_methods() {
	register_method("_ready", &Root::_ready);
	register_method("_play_click1", &Root::_play_click1);
	register_method("_play_click2", &Root::_play_click2);
	register_method("_switch_to_new_playground", &Root::_switch_to_new_playground);
	register_method("_switch_to_playground", &Root::_switch_to_playground);
	register_method("_switch_to_menu", &Root::_switch_to_menu);
}

Root::Root() {
	
}
Root::~Root() {
	
}

void Root::_init() {

}
void Root::_ready() {
	Ref<PackedScene> backPanelRef = ResourceLoader::get_singleton()->load("res://scenes/BackPanel.tscn");
	add_child(backPanelRef->instance());
	get_node<AudioStreamPlayer>("BackPanel/BGM")->play(0);
	Ref<PackedScene> mainMenuRef = ResourceLoader::get_singleton()->load("res://scenes/MainMenu.tscn");
	add_child(mainMenuRef->instance());
	get_node<Control>("MainMenu/VBoxContainer/GridContainer/RowCount")->grab_focus();
}

void Root::_play_click1() {
	get_node<AudioStreamPlayer>("BackPanel/Click1")->play(0);
}
void Root::_play_click2() {
	get_node<AudioStreamPlayer>("BackPanel/Click2")->play(0);
}

void Root::_switch_to_new_playground(const unsigned short rows, const unsigned short cols) {
	get_node<AnimationPlayer>("BackPanel/AnimationPlayer")->set_speed_scale(0.125);
	Ref<PackedScene> playgroundRef = ResourceLoader::get_singleton()->load("res://scenes/Playground.tscn");
	Node* playground = playgroundRef->instance();
	playground->call("setup", rows, cols);
	add_child(playground);
	playground->connect("backToMenu", this, "_switch_to_menu");
	get_node("MainMenu")->call("hide");
}
void Root::_switch_to_playground() {
	get_node<AnimationPlayer>("BackPanel/AnimationPlayer")->set_speed_scale(0.125);
	get_node("MainMenu")->call("hide");
	get_node("Playground")->call("show");
}

void Root::_switch_to_menu() {
	get_node<AnimationPlayer>("BackPanel/AnimationPlayer")->set_speed_scale(1);
	get_node("MainMenu")->call("show");
	get_node("Playground")->call("hide");
	get_node("Playground")->queue_free();
}